package one.stand.controller;

import com.jmp.api.dto.CategoryChildRequest;
import com.jmp.api.dto.CategoryChildResponse;
import com.jmp.api.dto.CategoryRequest;
import com.jmp.api.dto.CategoryResponse;
import com.jmp.feign.BannerFeign;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.BannerListDto;
import com.jmp.service.api.admin.dto.BannerListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.model.GoodsType;
import one.stand.model.ResultModel;
import one.stand.service.GoodsTypeService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "APP-分类")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private BannerFeign bannerFeign;

    @Autowired
    private GoodsTypeService goodsTypeService;

    @ApiOperation("一级分类")
    @GetMapping
    public ResultModel<CategoryResponse> index(@RequestAttribute CategoryRequest request) {

        List<GoodsType> goodsTypes = goodsTypeService.getParent(GoodsTypeService.PLATFORM_COMPANY_ID);
        List<CategoryResponse.Type> goodsTypeList = ConvertUtils.list(goodsTypes, goodsType -> {
            return CategoryResponse.Type.builder()
                    .typeId(goodsType.getGoodstypeId())
                    .typeName(goodsType.getName())
                    .build();
        });

        BannerListDto bannerListDto = BannerListDto.builder()
                .companyId(BannerApi.COMPANY_ID_PLATFORM)
                .bannerType(BannerApi.BANNER_TYPE_JSX_TYPE)
                .refId(null)
                .build();
        bannerListDto.setAll();
        ResultModel<BannerListVo> bannerListVoResultModel = bannerFeign.list(bannerListDto);
        AssertUtil.isTrue(bannerListVoResultModel);
        List<CategoryResponse.Banner> bannerList = ConvertUtils.list(bannerListVoResultModel.getData().getBannerPage().getList(), companyBanner -> CategoryResponse.Banner.builder()
                .source(companyBanner.getSource())
                .event(companyBanner.getEvent())
                .eventId(String.valueOf(companyBanner.getId()))
                .build());

        CategoryResponse response = CategoryResponse.builder()
                .typeList(goodsTypeList)
                .bannerList(bannerList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("二级分类")
    @GetMapping("/child")
    public ResultModel<CategoryChildResponse> goods(@RequestAttribute CategoryChildRequest request) {

        List<GoodsType> goodsTypes = goodsTypeService.getChild(request.getTypeId(), null);
        List<CategoryChildResponse.Type> goodsTypeList = ConvertUtils.list(goodsTypes, goodsType -> {
            return CategoryChildResponse.Type.builder()
                    .typeId(goodsType.getGoodstypeId())
                    .typeName(goodsType.getName())
                    .photo(goodsType.getPhoto())
                    .build();
        });

        CategoryChildResponse response = CategoryChildResponse.builder()
                .typeList(goodsTypeList)
                .build();
        return ResultModel.success(response);
    }
}
